package com.gt.quality.system.controller;


import com.gt.quality.base.BaseCommonController;
import com.gt.quality.base.BaseCommonService;
import com.gt.quality.system.entity.FileExportTemplate;
import com.gt.quality.base.pojo.Resp;
import com.gt.quality.system.service.FileExportTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件导入模板(FileExportTemplate)表控制层
 *
 * @author makeJava
 * @since 2025-01-04 22:14:06
 */
@RestController
@RequestMapping("fileExportTemplate")
@Tag(name = "文件导入模板")
public class FileExportTemplateController extends BaseCommonController<FileExportTemplate> {

    /**
     * 服务对象
     */
    private FileExportTemplateService fileExportTemplateService;

    @Autowired
    public void setFileExportTemplateService(FileExportTemplateService service) {
        this.fileExportTemplateService = service;
    }

    @Override
    public BaseCommonService<FileExportTemplate> getThisService() {
        return fileExportTemplateService;
    }

    @Override
    public boolean isSuperExcel() {
        return false;
    }

    /**
     * Description: 模板导入数据库表
     *
     * @author: makeJava
     * @date: 2025-01-04 22:20:01
     */
    @PostMapping(value = "/api/upload")
    @Operation(summary = "文件系统上传文件")
    @Parameters(value = {
            @Parameter(name = "templateName", description = "模板名称", in = ParameterIn.QUERY),
            @Parameter(name = "templateType", description = "模板类型", in = ParameterIn.QUERY),
            @Parameter(name = "templateVersion", description = "模板版本", in = ParameterIn.QUERY),
            @Parameter(name = "remark", description = "模板描述", in = ParameterIn.QUERY),
    })
    public Resp<Object> uploadFile(
            @RequestParam("templateName") String templateName,
            @RequestParam("templateType") String templateType,
            @RequestParam("templateVersion") String templateVersion,
            @RequestParam("remark") String remark,
            @RequestParam("file") MultipartFile file) throws IOException {
        FileExportTemplate fileExportTemplate = new FileExportTemplate();

        fileExportTemplate.setTemplateName(templateName);
        fileExportTemplate.setTemplateType(templateType);
        fileExportTemplate.setTemplateVersion(templateVersion);
        fileExportTemplate.setRemark(remark);
        byte[] bytes = file.getBytes();
        fileExportTemplate.setTemplateContext(bytes);
        fileExportTemplateService.save(fileExportTemplate);
        return Resp.ok(fileExportTemplate.getTemplateId());
    }

}
